package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import com.hmdp.utils.SystemConstants;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
//设置了拦截器，就要对拦截器进行配置
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/shop/**",
                        "/voucher/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/blog/hot",
                        "/blog/of/user",
                        "/user/code",
                        "/user/login",
                        "/voucher-order/**",
                        "/hmdp/**"  // 排除图片资源路径
                ).order(1);
        // token刷新的拦截器
//        .order(0)的作用是拦截器的执行顺序，0是最大，越大就越往后执行
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置图片资源映射：将 /hmdp/** 路径映射到本地图片目录
        registry.addResourceHandler("/hmdp/**")
                .addResourceLocations("file:" + SystemConstants.IMAGE_UPLOAD_DIR);
    }
}
